<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
		<title>Buyer Page</title>
		<link rel="stylesheet" href="../../css/styles.css" type="text/css" />
		<script type="text/javascript" src="js/validation.js"></script>
	</head>
	<script type="text/javascript">
	  
	  function showEnterForm(){
	      document.getElementById("enterDiv").style.display = "block";
	      document.getElementById("viewDiv").style.display = "none";
	  }
	  
	  function showSearchForm(){
	      document.getElementById("enterDiv").style.display = "none";
	      document.getElementById("viewDiv").style.display = "block";
	  }
	  
	  function setDisplay(){
	     var getDetailsField = document.getElementById("getDetailsField");
	     var saveDetailsField = document.getElementById("saveDetailsField");
	     if(getDetailsField != null){
	        showSearchForm();
	     }else if(saveDetailsField != null){
	       showEnterForm();
	     }
	  }
	   function validateFormOnSubmit(theForm) {
		var reason = "";
		
		 reason += validateFirstname(theForm.buyerFirstName);
		  reason += validateLastname(theForm.buyerLastName);
		/* reason += validateFirstname(theForm.buyerFirstName);
		reason += validateLastname(theForm.buyerLastName);
		reason += validateAddressLine(theForm.vendorAddress1);
		reason += validateAddressLine(theForm.vendorAddress2); */
		reason += validateCity(theForm.buyerCity);
		reason += validateCountry(theForm.buyerCountry);
		reason += validatePin(theForm.buyerPin);
		reason += validatePhone(theForm.buyerPhone);
		reason += validateEmail(theForm.buyerEmail);
		if (reason != "") {
	    alert("Some fields need correction:\n" + reason);
	    return false;
	  }
	 return true;
	}
	function validateFormOnSearch(theForm){
		var reason = "";
		
		reason +=validateSelectBuyer(theForm.bnameSearch);
		if (reason !=""){
		alert("Error Message:\n" + reason);
		return false;
		}
		return true;
	}
	</script>
<body onLoad="javascript:setDisplay();">
	<div id="container">
		<jsp:include page="/jsp/menu.jsp" />
                <div style="height:420px;">
			  		<div class="path">
						<br><a href="home.htm"> Home </a>-&gt;
						<a> Account </a>-&gt;
						<a> Sale </a>-&gt;
						<a href="buyer.htm"> Buyer Details </a>
			  		</div>
			  		<div id="leftDiv" style="margin-left: 10px;margin-top: 20px">
			   			<b>Buyer Details</b><br><br>
						<fieldset style="height:80px; width:200px;background: #FFF;">
						<table cellpadding=20>
						<tr>
							<td>
								<a href="javascript:showSearchForm()">
			                        	View Buyer Details
								</a>
							</td>
						</tr>
						<tr>
							<td>
			                 	<a href="javascript:showEnterForm()">
			                        	Enter Buyer Details
			                 	</a>
							</td>
						</tr>
						</table>
						</fieldset>
			  		</div>
				</div>
				<form:form name="form" method="POST" action="getBuyerDetails.htm" commandName="buyerDetails" onsubmit="return validateFormOnSearch(this)">
                 <div id="viewDiv" class="maininfo" style="display: none">
                 	<div class="lable">
						Buyer Details
					</div>
						<div id="searchForm">
						    <table>
							<tr>
								<td>Enter Buyer Name <span class="mand-star">*</span></td>
								<td><form:select path="bnameSearch">
										<form:option value="" label="------Select------" ></form:option>
										<c:forEach items="${model.bNameList}" var="buyer">
											<form:option value="${buyer.strBuyerName}">${buyer.strBuyerName}</form:option>
										</c:forEach>
								</form:select>
								</td>
							</tr>
							<tr>
								<td></td>
								<td><input type="submit" value="Search" size=5></td>
							</tr>
						</table>
						</div>
						<c:if test="${!empty model.bList}">
						<div id="viewTable">
							<table cellpadding="10">
							<tr>
								<th> BuyerID </th>
								<th> BuyerName </th>
								<th> BuyerAddress </th>
								<th> Contact No. </th>
							</tr>
						    <c:forEach items="${model.bList}" var="buyer">
						      <tr>
						        <td><c:out value="${buyer.buyerId}"></c:out> </td>
						         <td><c:out value="${buyer.buyerName}"></c:out> </td>
						         <td><c:out value="${buyer.buyerAddress}"></c:out> </td>
						         <td><c:out value="${buyer.buyerPhone}"></c:out> </td>
						      </tr>
						    </c:forEach> 	
						</table>
						</div>
						</c:if>

				  </div>
				 </form:form>
				 <form:form name="form1" method="POST" action="saveBuyerDetails.htm" commandName="buyerDetails" onsubmit="return validateFormOnSubmit(this)">
			 		<div id="enterDiv" class="maininfo" style="display: none">
			 			<div class="lable">
							Enter Buyer Details
							<c:if test="${model.isSave}">
								<div id="result"> <c:out value="Submitted Successfully"></c:out></div>
							</c:if>
						</div>
						<table>
							<tr>	
								<th> First Name <span class="mand-star">*</span> </th>
								<td><form:input path="buyerFirstName" /></td>
							</tr>
							<tr>	
								<th> Last Name <span class="mand-star">*</span> </th>
								<td><form:input path="buyerLastName" /></td>
							</tr>							
	
								<!-- <tr>
									<th> Buyer Address : </th>
									<td><form:input path="buyerAddress" /></td>
								</tr>
							<tr>	
								<th> Name <span class="mand-star">*</span> </th>
								<td><form:input path="buyerName" /></td>
							</tr>-->
							<tr> 
								<th> Address Line1 </th>
								<td><form:input path="buyerAddress1" /></td>
							</tr>
							<tr>
								<th> Address Line2 </th>
								<td><form:input path="buyerAddress2" /></td>
							</tr>
							<tr>
								<th> City <span class="mand-star">*</span> </th>
								<td><form:input path="buyerCity" /></td>
							</tr>
							<tr>
								<th> Country <span class="mand-star">*</span> </th>
								<td><form:input path="buyerCountry" /></td>
							</tr>
							<tr>
								<th> Pin No. <span class="mand-star">*</span> </th>
								<td><form:input path="buyerPin" maxlength="6"/></td>
							</tr>
							<tr>
								<th> Contact No. <span class="mand-star">*</span> </th>
								<td><form:input path="buyerPhone" maxlength="11"/></td>
							</tr>		
							<tr>
								<th> Email Id <span class="mand-star">*</span> </th>
								<td><form:input path="buyerEmail" /></td>
							</tr>		
							<tr>	              
								<th></th>
								<td><input type="submit" name="submit"  value="Submit"></td>
							</tr>
						</table>
					</div>
				</form:form>
			
			<c:if test="${model.isGet}">
			   <input id="getDetailsField" type="hidden" name="getDetailsField"/>  
			</c:if>
			<c:if test="${model.isSave}">
			   <input id="saveDetailsField" type="hidden" name="saveDetailsField"/>  
			</c:if>		
			<div id="footer">
				<jsp:include page="/jsp/footer.jsp" />
			</div>
	</div>
</body>
</html>
